<?php

namespace app\controllers;

use Yii;
use app\models\GkyWeArticleAnswer;
use app\models\GkyWeArticleAnswerSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * GkyWeArticleAnswerController implements the CRUD actions for GkyWeArticleAnswer model.
 */
class GkyWeArticleAnswerController extends Controller {
    public $enableCsrfValidation = false;
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all GkyWeArticleAnswer models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new GkyWeArticleAnswerSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single GkyWeArticleAnswer model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new GkyWeArticleAnswer model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new GkyWeArticleAnswer();

//        if ($model->load(Yii::$app->request->post()) && $model->save()) {
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            $model->content = $post['content'];
            $model->fan_name = $post['username'];
            $model->fan_open_id = $post['openid'];
            $model->answer_time = date('Y-m-d h:m:s');
            $model->articel_id = intval($post['artid']);
            if ($model->save()) {
                header("Access-Control-Allow-Origin: *");
                echo 1;
            } else {
                header("Access-Control-Allow-Origin: *");
                echo -1;
            }
        }
    }

    /**
     * Updates an existing GkyWeArticleAnswer model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }
    public function actionPraise($time){
        $answer= GkyWeArticleAnswer::findOne(['answer_time'=>$time]);
        $answer->praise_count+=1;
        if($answer->save()){
            header("Access-Control-Allow-Origin: *");
            echo $answer->answer_time;;
        }else{
            header("Access-Control-Allow-Origin: *");
            echo -1;
        }
    }
    //根据文章id获取评论
    public function actionGetAnswers($ariid) {
//        $ariid=  Yii::$app->request->get('ariid');
        $result = (new \yii\db\Query())
                ->select('*')
                ->from('gky_we_article_answer')
                
                ->where(['articel_id'=>  intval($ariid)])
                ->orderBy([
                    'praise_count'=>SORT_DESC,
                    'answer_time' => SORT_DESC,
                ])
                ->all();
        header("Access-Control-Allow-Origin: *");
        echo json_encode($result);
    }

    /**
     * Deletes an existing GkyWeArticleAnswer model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the GkyWeArticleAnswer model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return GkyWeArticleAnswer the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = GkyWeArticleAnswer::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

}
